package cl.indumotora.induapprecepcion.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sybase.collections.GenericList;

import cl.indumotora.induapprecepcion.dao.AgendaDAO;
import cl.indumotora.induapprecepcion.mbo.MBO_AgendaDiaria;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class VerAgendaActivity extends Activity {

	ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ver_agenda);
		
		// get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);
        
        // preparing list data
        prepareListData();
        
        listAdapter = new ListAdapterAgenda(this, listDataHeader, listDataChild);
        
        expListView.setAdapter(listAdapter);
        
     // Listview Group click listener
        expListView.setOnGroupClickListener(new OnGroupClickListener() {
 
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                 Toast.makeText(getApplicationContext(),
                 "Group Clicked " + listDataHeader.get(groupPosition),
                 Toast.LENGTH_SHORT).show();
                return false;
            }
            
           
        });
 
        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
 
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
            }
        });
 
        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
 
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();
 
            }
        });
 
        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {
        	@Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                        listDataHeader.get(groupPosition)).get(
                                        childPosition), Toast.LENGTH_SHORT)
                        .show();
                return false;
            }
        });
	}
	
	
	private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();
 
        GenericList<MBO_AgendaDiaria> ListAgenda;
        AgendaDAO agenda = new AgendaDAO();
        ListAgenda = agenda.AgendaDiaria();
        
        for (int i=0;i<ListAgenda.size();i++){
        	listDataHeader.add(ListAgenda.get(i).getHORA() + "-" + ListAgenda.get(i).getNOMBRE());
        	
        	List<String> detalleAgenda = new ArrayList<String>();
        	detalleAgenda.add("Hora : " + ListAgenda.get(i).getHORA());
        	detalleAgenda.add("Cliente : " + ListAgenda.get(i).getNOMBRE());
        	detalleAgenda.add("Patente : " + ListAgenda.get(i).getPATENTE());
        	listDataChild.put(listDataHeader.get(i), detalleAgenda);
        }
        
      
    }
	
	
	
}
